<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    request.setAttribute("basePath",basePath);
%>
<!DOCTYPE>
<html>
<head>
    <title>二维码生成</title>
    <%@ include file="/resources/js/easyui/taglibs.jsp"%>
    <%@ include file="/resources/js/easyui/_easyui.jsp"%>
    <%@ include file="/resources/js/easyui/_message.jsp"%>
    <%@ include file="/resources/js/easyui/_viewJS.jsp"%>
</head>

<body class="easyui-layout">
    <div>
        <table class="tableForm" style="width: 100%;">
            <!-- 设备二维码 s -->
            <form id="addDeviceForm">
                <tr>
                    <td colspan="2"><b>生成二维码</b></td>
                </tr>
                <%--<tr>
                    <th align="right" valign="top"><span class="redStart">*</span><label>设备短码前缀：</label></th>
                    <td><input class="easyui-validatebox easyui-textbox" name="code" id="deviceCode" style="width:200px" data-options="required:true,validType:'CheckDevicePrefix'" /></td>
                </tr>--%>
                <tr>
                    <th align="right" valign="top"><span class="redStart">*</span><label>数量：</label></th>
                    <td>
                        <input class="easyui-validatebox easyui-numberbox" name="codeNum" id="deviceCodeNum" style="width:200px" data-options="required:true,validType:'PositiveInteger',max:1000" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="hidden" name="type" id="deviceType" value="1"/>
                        <a class="easyui-linkbutton" href="javascript:void(0)" id="addDeviceBtn">生成二维码</a>
                        <a class="easyui-linkbutton" href="javascript:void(0)" id="downDeviceBtn" style="display: none">下载二维码</a>
                    </td>
                </tr>
            </form>
            <!-- 设备二维码 e -->
            <!-- 支付二维码 s -->
            <%--<form id="addPayForm">
                <tr>
                    <td colspan="2"><b>生成支付二维码</b></td>
                </tr>
                &lt;%&ndash;<tr>
                    <th align="right" valign="top"><span class="redStart">*</span><label>支付唯一码：</label></th>
                    <td><input class="easyui-validatebox easyui-textbox" name="code" id="payCode" style="width:200px" data-options="required:true,validType:'CheckPayPrefix'" /></td>
                </tr>&ndash;%&gt;
                <tr>
                    <th align="right" valign="top"><span class="redStart">*</span><label>数量：</label></th>
                    <td>
                        <input class="easyui-validatebox easyui-numberbox" name="codeNum" id="payCodeNum" style="width:200px" data-options="required:true,validType:'PositiveInteger',max:1000" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="hidden" name="type" id="payType" value="2"/>
                        <a class="easyui-linkbutton" href="javascript:void(0)" id="addPayBtn">生成二维码</a>
                        <a class="easyui-linkbutton" href="javascript:void(0)" id="downPayBtn" style="display: none">下载二维码</a>
                    </td>
                </tr>
            </form>--%>
            <!-- 支付二维码 e -->
        </table>
    </div>

<script type="text/javascript">
    function View(){
        var that = this;

        that.events = [
            { sel : '#addDeviceBtn', act : 'click', func : 'checkUserShortCode' },
            { sel : '#downDeviceBtn', act : 'click', func : 'downDeviceSub' },
            { sel : '#addPayBtn', act : 'click', func : 'addPaySub' },
            { sel : '#downPayBtn', act : 'click', func : 'downPaySub' }
        ];

        var codeNum = '';
        var type = '';

        that.checkUserShortCode = function () {
            $.ajax({
                url:'${basePath}web/RQCode/checkUserShortCode',
                type : 'GET',
                async : false,
                traditional:true,
                error: function (response) {
                    ViewHelper.func.ajaxErrorMessage(response);
                },
                success: function (ret) {
                    if(ret.result) {
                        that.addDeviceSub();
                    }else{
                        $.messager.alert('提示','您没有用户短码，请联系管理员!','error');
                    }
                }
            });
        };

        that.addDeviceSub = function(){
            codeNum = $("#deviceCodeNum").textbox('getValue');
            type = $("#deviceType").val();
            if($("#addDeviceForm").form('validate')){
                $.ajax({
                    url:'${basePath}web/RQCode/add',
                    type : 'POST',
                    data : {"codeNum":codeNum,"type":type},
                    async : false,
                    traditional:true,
                    error: function (response) {
                        ViewHelper.func.ajaxErrorMessage(response);
                    },
                    success: function (ret) {
                        if(ret.result) {
                            $.messager.alert('提示信息！',ret.info,'info');
                            $("#downDeviceBtn").css("display","");
                        }else{
                            $("#downDeviceBtn").css("display","none");
                            $.messager.alert('提示','生成失败!','error');
                        }
                    }
                });
            }
        };

        that.downDeviceSub = function(){
            type = $("#deviceType").val();
            $.ajax({
                url:'${basePath}web/RQCode/checkFile/'+type,
                type : 'GET',
                async : false,
                traditional:true,
                error: function (response) {
                    ViewHelper.func.ajaxErrorMessage(response);
                },
                success: function (ret) {
                    if(ret.result) {
                        window.location.href = '${basePath}web/RQCode/download/'+type;
                    }else{
                        $.messager.alert('提示','下载失败!','error');
                    }
                }
            });
        };

        that.addPaySub = function(){
            codeNum = $("#payCodeNum").textbox('getValue');
            type = $("#payType").val();
            if($("#addPayForm").form('validate')){
                $.ajax({
                    url:'${basePath}web/RQCode/add',
                    type : 'POST',
                    data : {"codeNum":codeNum,"type":type},
                    async : false,
                    traditional:true,
                    error: function (response) {
                        ViewHelper.func.ajaxErrorMessage(response);
                    },
                    success: function (ret) {
                        if(ret.result) {
                            $.messager.alert('提示信息！',ret.info,'info');
                            $("#downPayBtn").css("display","");
                        }else{
                            $("#downPayBtn").css("display","none");
                            $.messager.alert('提示','生成失败!','error');
                        }
                    }
                });
            }
        };

        that.downPaySub = function(){
            type = $("#payType").val();
            $.ajax({
                url:'${basePath}web/RQCode/checkFile/'+type,
                type : 'GET',
                async : false,
                traditional:true,
                error: function (response) {
                    ViewHelper.func.ajaxErrorMessage(response);
                },
                success: function (ret) {
                    if(ret.result) {
                        window.location.href = '${basePath}web/RQCode/download/'+type;
                    }else{
                        $.messager.alert('提示','下载失败!','error');
                    }
                }
            });
        };

        EventHandler.call(that, that.events);

        return that;
    }

    var view;

    $(function(){
        inherit(View, EventHandler);
        view = new View();
    });

    $.extend($.fn.validatebox.defaults.rules, {
        PositiveInteger: {
            validator: function (value) {
                var re = new RegExp(/^\d+$/);
                if (value.match(re) != null) {
                    return true;
                }
                return false;
            },
            message: '正整数输入不合法,最大值为1000'
        },
        CheckDevicePrefix: {
            validator: function (value) {
                var re = new RegExp(/^[0-9a-zA-Z]{4}$/);
                if (value.match(re) != null) {
                    return true;
                }
                return false;
            },
            message: '最多四个字符,只能是字母或数字'
        },
        CheckPayPrefix: {
            validator: function (value) {
                var re = new RegExp(/^[0-9a-zA-Z]{0,10}$/);
                if (value.match(re) != null) {
                    return true;
                }
                return false;
            },
            message: '最多十个字符,只能是字母或数字'
        }
    });

</script>

</body>
</html>
